/*
 * Create in 2012-12-3 下午10:27:51.
 */
package kit4j.app.notepad.ui.menu;

import java.awt.MenuItem;
import java.util.List;

import kit4j.app.notepad.command.NotepadCommandExecutor;
import kit4j.app.notepad.command.ShowAllNotepadCommand;
import kit4j.app.notepad.config.NotepadConfig;
import kit4j.app.notepad.factory.NotepadFactory;
import kit4j.app.notepad.factory.listener.NotepadFactoryListener;
import kit4j.app.notepad.ui.listener.ExecuteCommandActionListener;
import kit4j.app.notepad.ui.window.Notepad;

/**
 * 
 * @author MiniKnife
 * 
 */
public class ShowAllNotepadMenuItem extends MenuItem {

	private static final long serialVersionUID = -8697578382671124312L;

	public ShowAllNotepadMenuItem(NotepadConfig config, NotepadCommandExecutor commandExecutor) {
		super(config.getString("MENU.SHOW_ALL_LABEL", "Show All"));
		initListener(commandExecutor);
	}

	/**
	 * 
	 * @throws NullPointerException
	 */
	private void initListener(NotepadCommandExecutor commandExecutor) {
		addActionListener(new ExecuteCommandActionListener(new ShowAllNotepadCommand(), commandExecutor));
		NotepadFactory.getInstance().addNotepadFactoryListener(new NotepadFactoryListener() {

			@Override
			public void notepadShowed(Notepad notepad) {
				resetShowAllNotepadMenuItem();
			}

			@Override
			public void notepadFreed(Notepad notepad) {
				resetShowAllNotepadMenuItem();
			}
		});
	}

	private void resetShowAllNotepadMenuItem() {
		List<Notepad> notepadList = NotepadFactory.getInstance().getShowingNotepadList();
		setEnabled(!notepadList.isEmpty());
	}

}
